{% extends "base.html" %}
{% from '_helpers.html' import external_bugs %}
{% from '_helpers.html' import external_urls %}
{% from '_helpers.html' import metric_table %}
{% from '_helpers.html' import unique_metric_table %}
{% from '_helpers.html' import package_counts_table %}
{% from '_helpers.html' import history_graph %}
{% from '_helpers.html' import release_graph %}
{% from '_helpers.html' import show_backtrace %}

{% block title -%}
  {{ "Report #%d - %s" | format(report.id, component.name) -}}
  {{ " in %s" | format(report.backtraces[0].crash_function) | truncate(80, True) if report.backtraces | length > 0 }}
{%- endblock %}

{% block js %}
<script src="{{ url_for('static', filename='js/color.js')}}"></script>
<script src="{{ url_for('static', filename='js/jquery.flot.js')}}"></script>
<script src="{{ url_for('static', filename='js/jquery.flot.pie.js')}}"></script>
<script src="{{ url_for('static', filename='js/jquery.flot.stack.js')}}"></script>
<script src="{{ url_for('static', filename='js/jquery.flot.time.js')}}"></script>
{% endblock js %}

{% block body %}
  <div class='report'>
    <div class='row'>
      <div class='col-md-6'>
        {% if report.reasons %}
          <h3>{{ report.reasons[0] }}</h3>
        {% else %}
          <h3>Info</h3>
        {% endif %}
        {% if unpackaged %}
          Note: Executable of this report does not come from a package.
        {% endif %}
        <dl class='info dl-horizontal'>
          <dt>Similar reports
            <a role="button" data-toggle="tooltip" title="Reports with similar backtraces are grouped together into problems.">
              <span class="fa fa-lg fa-info-circle">
            </a>
          </dt>
          <dd>
            {% if report.problem_id %}
              <a href="{{ url_for('problems.item', problem_id=report.problem_id) }}"
                >Problem #{{ report.problem_id }}</a>
            {% else %}
              <em>An owning problem doesn't exist yet.</em>
            {% endif %}
          </dd>
          {% if extfafs %}
            <dt>Ext. instances</dt>
            <dd>
              {% for extfaf in extfafs %}
                {# In the very rare case when a report has multiple hashes, we must show them all#}
                {%- for db_hash in report.hashes %}
                  <a href="{{extfaf.baseurl}}/reports/bthash/{{db_hash.hash}}/">{{extfaf.name}}</a>{% if not loop.last %}, {% endif %}
                {%- endfor -%}
                {%- if not loop.last %}, {% endif %}
              {% endfor %}
            </dd>  
          {% endif %}
          <dt>Component</dt>
          <dd>
            {{ component.name }}
            {% if report.tainted %}
              <span class="label label-warning">Tainted</span>
            {% endif %}
          </dd>
          <dt> Last affected version </dt>
          <dd>
            {{ version }}
          </dd>
            <dt>Executable</dt>
            <dd>
              {{ executable }}
            </dd>
          {% if report.error_name %}
            <dt>Error name</dt>
            <dd>
              {{ report.error_name }}
            </dd>
          {% endif %}
          <dt>First occurrence</dt>
          <dd>{{ report.first_occurrence.strftime("%Y-%m-%d")}}</dd>
          <dt>Last occurrence</dt>
          <dd>{{ report.last_occurrence.strftime("%Y-%m-%d")}}</dd>
          {% if solutions %}
          {#
            I know, now can be only one solution, but this is preparation
            to future, when we want allow have a multiple solution
          #}
          <dt>Solution</dt>
          <dd>
              {% for solution in solutions %}
                {{ solution.note_html }}<br>
              {% endfor %}
          </dd>
          {% endif %}
          <div class="unique_data">
            <dt>Unique reports</dt>
            <dd>
              {% for name, count in unique_ocurrence_os.items() %}
                {%- set unique_count = unique_count|default(0,true) + count.unique|default(0,true) -%}
                {% if loop.last %}
                  {{ unique_count|readable_int }}
                {% endif %}
              {% endfor %}
            </dd>
          </div>
          <dt class="hide">Quality</dt>
          <dd class="hide">{{ report.quality }}</dd>
          {% if report.bugs %}
            <dt>External bugs</dt>
            <dd>
              {{ external_bugs(report.id, report.bugs, is_maintainer) }}
            </dd>
          {% endif %}
          {% if contact_emails %}
            <dt>Contact emails</dt>
            <dd>
              {% for email in contact_emails %}
                <a href="mailto:{{email}}">{{email}}</a>
              {% endfor %}
            </dd>
          {% endif %}
          {% if report.urls %}
            <dt>External URLs
              <a role="button" data-toggle="tooltip" title="Ordered by newest URL">
                <span class="fa fa-lg fa-info-circle">
              </a>
            </dt>
            <dd>
              <ul>
                {{ external_urls([report.urls]) }}
              </ul>
            </dd>
          {% endif %}
        </dl>
        {% if is_maintainer %}
          <a href="{{url_for('reports.associate_bug', report_id=report.id)}}" class="btn btn-default">Associate bug</a>
          <div id="archive-data" class={% if report.archived %}"well"{% else %}"well-muted"{% endif %}>
            <span id="archive-msg" {% if not report.archived %}class="text-muted"{% endif %}>
                {% if report.archived %}
                  This report was archived {{ report.archive.date }} by {{ report.archive.username }}.
                  If you want to see it on reports dashboard restore it from archive.
                {% else %}
                  If you don't consider this report useful, you can archive
                  it and it won't be displayed on reports dashboard.
                {% endif %}
            </span>
            <button id="archive-btn" class="btn btn-default">{% if report.archived %}Restore{% else %}Archive{% endif %}</button>
          </div>
          <script type="text/javascript">
            var callback = function updatePage(result) {
              if (result.status == 'failure') {
                alert('Archive action failed.');
              } else {
                var archive_btn = document.getElementById('archive-btn').firstChild;
                if (archive_btn.data == 'Archive') {
                  archive_btn.data = 'Restore';
                  $('#archive-msg').text(
                          'This report was archived ' + result.date + ' by ' +
                          result.username +
                          '. If you want to see it on reports dashboard restore it from archive.');
                  $('#archive-data').toggleClass('well-muted well');
                  $('#archive-msg').attr('class', '');
                } else {
                  archive_btn.data = 'Archive';
                  $('#archive-msg').text(
                          'If you don\'t consider this report useful, you can ' +
                          'archive it and it won\'t be displayed on reports dashboard.');
                  $('#archive-data').toggleClass('well well-muted');
                  $('#archive-msg').attr('class', 'text-muted');
                }
              }
            }

            function archive() {
              var archived = {{report.archived | tojson}};
              var data = {'activate': !archived};
              var url = 'archive.json';
              postData(url, data, callback);
            } document.getElementById('archive-btn').onclick = archive;
         </script>
        {% endif %}

        {# graphs #}

        {% if releases|length > 1 or arches|length > 1 %}
          <input id="switch_data" class="btn btn-default" type="button" value="Hide unique" data-show="1">
          <h3>Graphs</h3>
          <div class='row'>
            <div class="unique_data">
              {{ release_graph(complete_history, releases, True) }}
            </div>
            <div class="original_data">
              {{ release_graph(complete_history, releases, False) }}
            </div>
            <div id="arch_graph" class="col-md-6 graph"></div>
          </div>
          <script>
            var arch_data = [];
            {% for arch, count in arches %}
              arch_data.push( {
                label: "{{ arch }}",
                data: {{ count }}, } );
            {% endfor %}

            var pie_chart_options =  {
                series: {
                  pie: {
                    show: true,
                  },
                },
              }

            {% if arches|length > 1 %}
              $.plot($('#arch_graph'), arch_data, pie_chart_options);
            {% endif %}
          </script>
        {% endif %}
      </div>

      {# metrics #}
      <div class='col-md-6 statistics'>
        <h3>Statistics</h3>
        <div class="unique_data">
          {{ unique_metric_table('Operating system', unique_ocurrence_os) }}
        </div>

        <div class="original_data">
        {{ metric_table('Operating system', releases) }}
        </div>

        {{ metric_table('Architecture', arches) }}

        {{ package_counts_table(package_counts) }}

        {% if package_counts %}
          <p class="text-muted">Packages names with count less than the total count of reports are most probably not the cause of the problem.</p>
        {% endif %}

        {{ metric_table('SELinux modes', modes) }}
      </div>
    </div>
    <div class="unique_data">
      <div class='row'>
        <div class='col-md-12'>
          <h3>History</h3>
        </div>
        <div class='col-md-4'>
          <h4>Daily:</h4>
          <div id="daily_unique_legend" class="col-md-12"></div>
        </div>
        <div class='col-md-4'>
          <h4>Weekly:</h4>
          <div id="weekly_unique_legend" class="col-md-12"></div>
        </div>
        <div class='col-md-4'>
          <h4>Monthly:</h4>
          <div id="monthly_unique_legend" class="col-md-12"></div>
        </div>
      </div>

      <div class='row'>
        <div class='col-md-4'>
          {{ history_graph(daily_history, "daily", "day", True) }}
        </div>
        <div class='col-md-4'>
          {{ history_graph(weekly_history, "weekly", "week", True) }}
        </div>
        <div class='col-md-4'>
          {{ history_graph(monthly_history, "monthly", "month", True) }}
        </div>
      </div>
    </div>

    <div class="original_data">
      <div class='row'>
        <div class='col-md-12'>
          <h3>History</h3>
        </div>
        <div class='col-md-4'>
          <h4>Daily:</h4>
          <div id="daily_legend" class="col-md-12"></div>
        </div>
        <div class='col-md-4'>
          <h4>Weekly:</h4>
          <div id="weekly_legend" class="col-md-12"></div>
        </div>
        <div class='col-md-4'>
          <h4>Monthly:</h4>
          <div id="monthly_legend" class="col-md-12"></div>
        </div>
      </div>
      <div class='row'>
        <div class='col-md-4'>
          {{ history_graph(daily_history, "daily", "day", False) }}
        </div>
        <div class='col-md-4'>
          {{ history_graph(weekly_history, "weekly", "week", False) }}
        </div>
        <div class='col-md-4'>
          {{ history_graph(monthly_history, "monthly", "month", False) }}
        </div>
      </div>
    </div>

    <br />

    <h3>Backtrace</h3>
    {{ show_backtrace(backtrace, report.type, report.oops)}}

    {# comments #}
    {% if is_maintainer and report.comments %}
      <h3>Comments <small>Attached by users who sent the report.</small></h3>
      <ul id="comments">
      {% set row_limit = 15 %}
      {% for comment in report.comments %}
        <li
        {% if loop.index > row_limit and comments|length > row_limit+1 %} {# avoid +1 more #}
          class='hide'
        {% endif %}
        >
          <p class="pre"><!-- comment id {{comment.id}} -->{{comment.text}}</p>
          <p><small>Comment saved {{comment.saved.strftime("%Y-%m-%d")}}</small></p>
        </li>
        {% if report.comments|length > row_limit+1 and loop.index == row_limit %}
          <script type="text/javascript">
            $(document).ready(function() {
              $("#comments .btn-more").click(function() {
                $(this).parents("ul").find('li.hide').removeClass('hide');
                $(this).parents("p").remove();
              });
            });
          </script>
          <p>
            <button class="btn btn-mini btn-more" type="button">
              + {{ report.comments|length-row_limit }} more
            </button>
          </p>
        {% endif %}
      {% endfor %}
      </ul>
    {% endif %}
  </div>
  <script>
    $(document).ready(function () {
      $(".original_data").hide();
    });

    $(document).on("click","#switch_data",function(){
        $(".unique_data").toggle();
        $(".original_data").toggle();
        if ($(this).data("show") == 1){ //First toggle
            $(this).val("Show unique");
            $(this).data("show", 0);
        }else{ // second toggle
            $(this).val("Hide unique");
            $(this).data("show", 1);
        }

    });
  </script>
{% endblock %}
